package com.example.myenglishbook.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myenglishbook.MainActivity;
import com.example.myenglishbook.R;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myenglishbook.database.Words;
import com.example.myenglishbook.utils.StringUtils;
import com.example.myenglishbook.viewModel.WordsViewModel;

import java.util.List;

public class ListRecyclerViewAdapter extends RecyclerView.Adapter<ListRecyclerViewAdapter.InnerHolder> {
    private List<Words> data;
    private int styleItem;
    private WordsViewModel wordsViewModel;
    private Context context;

    public ListRecyclerViewAdapter(List<Words> data,boolean isCardView,WordsViewModel wordsViewModel){
        this.data=data;
        this.wordsViewModel=wordsViewModel;
        if(isCardView){
            styleItem=R.layout.cell_card_recyler_view_item;
        }else{
            styleItem=R.layout.list_recycler_view_item;
        }
    }

    @NonNull
    @Override
    public ListRecyclerViewAdapter.InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(styleItem,null);
        context=parent.getContext();
        return new InnerHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ListRecyclerViewAdapter.InnerHolder holder, int position) {
        holder.aSwitch.setOnCheckedChangeListener(null);//初始时保证为空，保证稳定性
        holder.setData(data.get(position),position);

        if(holder.instance==0){
            holder.aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {//开关监听器
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    holder.showMeaning(isChecked,wordsViewModel);
                }
            });
            holder.instance=1;//已生成
        }
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    class InnerHolder extends RecyclerView.ViewHolder{
        TextView t1,t2,t3;
        Switch aSwitch;
        Words word;
        int instance=0;//判断监听器是否已生成

        InnerHolder(@NonNull View itemView) {
            super(itemView);
            t1=itemView.findViewById(R.id.itemNum);
            t2=itemView.findViewById(R.id.itemWord);
            t3=itemView.findViewById(R.id.itemMeaning);
            aSwitch=itemView.findViewById(R.id.itemSwitch);

            itemView.setOnClickListener(new View.OnClickListener() {//点击跳转到对于的页面
                @Override
                public void onClick(View v) {
                    Uri uri=Uri.parse("https://fanyi.baidu.com/translate?aldtype=16047&query=&keyfrom=baidu&smartresult=dict&lang=auto2zh#en/zh/"+t2.getText().toString());
                    Intent intent=new Intent(Intent.ACTION_VIEW);
                    intent.setData(uri);
                    itemView.getContext().startActivity(intent);
                }
            });

            itemView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    showUpdate();
                    return true;//true完成后结束执行
                }
            });
        }

        void setData(Words word,int position){
            t1.setText(position+1+"");
            t2.setText(word.getWord());
            t3.setText(word.getMeaning());
            if(word.getStatus()==0){//设置开关的状态
                aSwitch.setChecked(true);
                t3.setVisibility(View.GONE);
            }else{
                aSwitch.setChecked(false);
                t3.setVisibility(View.VISIBLE);
            }
            this.word=word;
        }

        void showMeaning(boolean isChecked,WordsViewModel wordsViewModel){
            if(isChecked){
                t3.setVisibility(View.GONE);
                word.setStatus(0);
            }else{
                t3.setVisibility(View.VISIBLE);
                word.setStatus(1);
            }
            wordsViewModel.update(word);
        }

        void showUpdate(){//更新视图
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            View view = LayoutInflater.from(context).inflate(R.layout.add_alert_dialog_view, null);
            EditText wordEditor = view.findViewById(R.id.wordEditor);
            EditText meaningEditor = view.findViewById(R.id.meaningEditor);
            builder.setTitle(R.string.update_word);
            builder.setView(view);
            builder.setNegativeButton(R.string.update, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if (StringUtils.isEmpty(wordEditor.getText().toString()) || StringUtils.isEmpty(meaningEditor.getText().toString())) {
                        Toast.makeText(context, R.string.warn_not_empty, Toast.LENGTH_SHORT).show();
                    } else {
                        word.setWord(wordEditor.getText().toString());
                        word.setMeaning(meaningEditor.getText().toString());
                        wordsViewModel.update(word);
                    }
                }
            });
            builder.setPositiveButton(R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                }
            });
            builder.create().show();
        }
    }
}
